package com.atguigu.tingshu.user.service.strategy.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.service.strategy.PaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: atguigu
 * @create: 2024-12-09 14:16
 */
@Slf4j
@Component("1003")  //默认@Component产生Bean对象是类名称（首字母小写） 故将Bean名称设置为跟客户端持有的商品类型一致
public class VIPPaidRecordStrategy implements PaidRecordStrategy {

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;



    /**
     * 虚拟物品发货-VIP会员
     * @param userPaidRecordVo
     */
    @Override
    public void handlerPaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("[用户服务]虚拟物品发货-VIP会员：{}", userPaidRecordVo);
        //3.处理虚拟物品发货商品类型为会员
        //2.1 判断该笔订单是否已经处理过 判断订单编号是否存在已购专辑表
        LambdaQueryWrapper<UserVipService> paidAlbumLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paidAlbumLambdaQueryWrapper.eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo());
        Long count = userVipServiceMapper.selectCount(paidAlbumLambdaQueryWrapper);
        if (count == 0) {
            //3.1 新增会员购买记录
            //3.1.1 获取用户信息，得到是否为会员
            Boolean isVIP = false;
            UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
            if (userInfo.getIsVip().intValue() == 1 && userInfo.getVipExpireTime().after(new Date())) {
                isVIP = true;
            }

            //3.1.2 获取会员配置信息，得到服务月数
            VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(userPaidRecordVo.getItemIdList().get(0));
            Integer serviceMonth = vipServiceConfig.getServiceMonth();

            //3.1.3 构建会员购买记录，保存会员购买记录
            UserVipService userVipService = new UserVipService();
            if (isVIP) {
                //3.1.3.1 如果是会员续费 “计算本次会员起始时间=当前会员过期时间+1天，跟本次会员过期时间
                Date currVipExpireTime = userInfo.getVipExpireTime();
                DateTime startTime = DateUtil.offset(currVipExpireTime, DateField.DAY_OF_MONTH, 1);
                userVipService.setStartTime(startTime);
                DateTime expireDate = DateUtil.offset(startTime, DateField.MONTH, serviceMonth);
                userVipService.setExpireTime(expireDate);
            } else {
                //3.1.3.2 如果是新开会员 计算本次会员起始时间，跟本次会员过期时间
                Date today = new Date();
                userVipService.setStartTime(today);
                DateTime expireDate = DateUtil.offset(today, DateField.MONTH, serviceMonth);
                userVipService.setExpireTime(DateUtil.endOfDay(expireDate));
            }
            userVipService.setUserId(userPaidRecordVo.getUserId());
            userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
            userVipServiceMapper.insert(userVipService);

            //3.2 更新会员状态及过期时间
            userInfo.setIsVip(1);
            userInfo.setVipExpireTime(userVipService.getExpireTime());
            userInfoMapper.updateById(userInfo);
        }
    }
}
